<?php
/* @var $this AnalysisController */

//所有购买会员的等级
$memtype = MemType::model()->getMemtype(1);
//条件数据处理
if(!empty($model->zcr_condition)){
	foreach($model->zcr_condition as $val){
		$condition[$val['condition']] = $val['value'];
	}
}

?>
<?php $form=$this->beginWidget('CActiveForm', array(
		'id'=>'proPromotion-form',
		'enableAjaxValidation'=>false,
		'htmlOptions'=>array('class'=>'create_box meetgive')
	)); ?>
	<ul class=" form_list2 form_list">
			<li class="c_f title"><h3 class="fs_14">积分产品</h3></li>
			<li class="c_f"><span class="tit">活动名称：</span>
				<div class="cont f_l">
					<?php echo $form->textField($model,'zcr_title',array('class'=>'t_ipt w_250','maxlength'=>10))?>
					<span id="CmpPromotion_zcr_titleTip"></span>
				</div>
			</li>
			<li class="c_f"><span class="tit">活动时间：</span>
				<div class="cont f_l">
					<?php echo $form->textField($model,'zcr_start_time',array('onclick'=>"WdatePicker({startDate:'%y-%M-01 00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true})"))?>
					<span class="c_999">-</span>
					<?php echo $form->textField($model,'zcr_end_time',array('onclick'=>"WdatePicker({startDate:'%y-%M-01 00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true})"))?>
					<span id="CmpPromotion_zcr_start_timeTip"></span>
				</div>
			</li>
			<li class="c_f"><span class="tit"><em class="xing">*</em>选择宝贝：</span>
				<div class="cont f_l">
					<div class="group_box">
						<table width="100%" cellpadding="0" cellspacing="0">
							<tr>
								<td width="22%" class="title">商品名称：<?php echo $form->hiddenField($model,'zp_id')?></td>
								<td width="78%" class="c_666" id="tuan_pro_name"><?php echo empty($model->product)?'请选择产品':$model->product->zp_name?></td>
							</tr>
							<tr class="">
								<td class="title">展示图片：</td>
								<td>
									<div class="c_img c_img120"><a href="javascript:" title="">
										<?php echo CHtml::image(empty($model->product)?'/images/cert_img.jpg.':$model->product->zp_pic,'',array('id'=>'img1','width'=>'120px','height'=>'120px'))?></a>
									</div>
								</td>
							</tr>
							<tr>
								<td class="title">产品原价：</td>
								<td>
									<span class="c_999 zp_price"><?php if(!empty($model->product)) echo $model->product->zp_price?></span>
								</td>
							</tr>
						</table>
					</div>
					<p class="p_5 "><a class="mem_bgx link_btn" onclick="addmatch()" href="javascript:" title="">选择产品<b class="mem_bgx left"></b><b class="mem_bgx right"></b></a><a class="mem_bgx link_btn" onclick="return addmatch();" href="javascript:" title="" style="display:none;">重新选择宝贝<b class="mem_bgx left"></b><b class="mem_bgx right"></b></a></p>
				</div>
			</li>
			<li class="c_f"><span class="tit">优惠内容：</span>
				<div class="cont f_l">
					<div class=" select">
						<div class="coupon" id="zcr_condition">
						<table class="list_table list_table2" width='100%' cellpadding="0" cellspacing="0">
							<?php if(!empty($memtype)){ foreach((array)$memtype as $key=>$val ){?>
							<tr>
								<td class="fs_14" width="20%">会员等级：</td>
								<td width="30%"><strong class="c_f60"><?php echo $val?></strong></td>
								<td width="20%"><?php echo CHtml::hiddenField('CmpPromotion[zcr_condition][condition][]',$key)?>购买积分：</td>
								<td width="30%"><strong class="c_f60"><?php echo CHtml::textField('CmpPromotion[zcr_condition][value][]',isset($condition)?$condition[$key]:'',array('class'=>'t_ipt value_input w_80'))?></strong><td>
							</tr>
							<?php }}?>
						</table>
						</div>
					</div>
				</div>
			</li>
			<li class="c_f"><span class="tit">优惠备注：</span>
				<div class="cont f_l">
					<?php echo $form->textArea($model,'zcr_content',array('class'=>'t_area'))?>
					<?php echo $form->error($model,'zcr_content')?>
				</div>
			</li>
			<li class="c_f save_link"><span class="tit">&nbsp;</span>
				<div class="cont f_l">
					<?php echo CHtml::submitButton(Yii::t('cmp','Save Set'),array('class'=>'mem_btn mem_btn20 mr5'))?>
				</div>
			</li>
		</ul>
<?php $this->endWidget(); ?>


<!-- 选择产品弹框star -->
<?php
	Yii::app()->clientScript->registerScript('search', "
		$('.search-form form').submit(function(){
			$.fn.yiiGridView.update('product-grid', {
				data: $(this).serialize()
			});
			return false;
		});
	");
?>
<div class="d_n" id="product_list">
<div class="d-title" style="display: block; ">选择产品</div>
<a class="d-close" href="javascript:" style="display: block;" onclick="addmatch()">×</a>
<div class="pro_filter c_f search-form">
<?php $form=$this->beginWidget('CActiveForm', array(
	'action'=>Yii::app()->createUrl($this->route),
	'method'=>'get',
)); ?>
	<label>产品标题：</label>
	<?php echo $form->textField($product,'zp_name',array('class'=>'t_ipt w_180'))?>
	<label>产品货号：</label>
	<?php echo $form->textField($product,'pro_no',array('class'=>'t_ipt w_180'))?>
	<?php echo CHtml::submitButton(Yii::t('cmp','Search'),array('class'=>'mem_bgx mem_btn01'))?>
<?php $this->endWidget(); ?>
</div>

<div style="height:300px;overflow-y:auto;">
<!-- 列表 -->
<?php 
	$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'product-grid',
	'dataProvider'=>$product->search(40),//数据源
	//'filter'=>$product,//设置过滤器，筛选输入框
	'template'=>'{items}',
	'enablePagination'=>false,
	'columns'=>array(
		array(
			'class' => 'CCheckBoxColumn',
			'checkBoxHtmlOptions' => array('name' => 'id[]','class'=>'chk d_n'),
			'htmlOptions'=>array('width'=>'6%'),
		),
		array('class'=>'CLinkColumn',
				'header'=>'产口图片',
				'labelExpression'=>'Tool::thumb($data->zp_pic,50,50,$data->zp_pic)',
				'urlExpression'=>'array("/home/product/detail","id"=>$data->zp_id)',
				'linkHtmlOptions'=>array('width'=>'50','height'=>'50'),
				'htmlOptions'=>array('width'=>'10%'),
		),
		array('class'=>'CLinkColumn',
				'header'=>'产品标题',
				'labelExpression'=>'mb_substr($data->zp_name,0,12,"utf8")',
				'urlExpression'=>'array("/home/product/detail","id"=>$data->zp_id)',
				'linkHtmlOptions'=>array('class'=>'c_06c'),
				'htmlOptions'=>array('width'=>'30%','class'=>'t_c'),
		),
		array('name'=>'pro_no',
				'value'=>'$data->pro_no',
				'htmlOptions'=>array('width'=>'10%','class'=>'t_c'),
		),
		array('name'=>'zp_price',
				'value'=>'$data->zp_price',
				'htmlOptions'=>array('width'=>'15%','class'=>'t_c'),
		),
		array('name'=>'zp_inventory',
				'value'=>'$data->zp_inventory',
				'htmlOptions'=>array('width'=>'15%','class'=>'t_c'),
		),
		array('class'=>'CLinkColumn',
				'header'=>'操作',
				'labelExpression'=>'"选择"',
				'urlExpression'=>'"javascript:"',
				'linkHtmlOptions'=>array('class'=>'mem_btn mem_btn13 pro_item'),
				'htmlOptions'=>array('width'=>'14%'),
		),
	)
));
?>
</div>
<div class="d-footer">
<div class="t_l f_l" id="select_pro" style="padding:10px 20px;">
<?php if(!empty($model->products)){ foreach($model->products as $val){?>
<span class="mr10 pro" id="select_pro_<?php echo $val->zp_id?>" pid="<?php echo $val->zp_id?>"><?php echo mb_substr($val->zp_name,0,16,'utf8')?></span>
<?php }}?>
</div>
<div class="d-buttons" style=""><input type="button" class="d-button d-state-highlight" value="确定" onclick="addmatch()"></div>
</div>
</div>
<script type="text/javascript">
//显示弹框
function addmatch(){
	$('#product_list').toggle();
}

//选择
$('.pro_item').live('click',function(){
	var pid = $(this).parent().parent().find('.chk').val()
	var pic = $(this).parent().parent().find('td').eq(1).find('img').attr('alt');
	var pnametext = $(this).parent().parent().find('td').eq(2).text();
	var pprice = $(this).parent().parent().find('td').eq(4).text();
	var inventory = $(this).parent().parent().find('td').eq(5).text();
	if($('#ProTuan_zp_id').val()==pid){
		art.dialog({title:'警告：',content:'相同产品请勿重复添加',okValue:"确定",ok:function(){return true;}});
		$(this).text('取消');
	}else{
		$(this).text('取消');
		$('#tuan_pro_name').text(pnametext);
		$('#img1').attr('src',pic);
		$('#proimage1').attr('value',pic);
		$('#CmpPromotion_zp_id').val(pid);
		$('#select_pro').html('<span class="mr10 pro" id="select_pro_'+pid+'" pid="'+pid+'">'+pnametext+'</span>');
		$('.zp_price').text(pprice);
		$('.zp_price_hidden').val(pprice);
		$('.zp_inventory').text(inventory);
		$(this).text('取消');
	}
})
</script>
<!-- 选择产品弹框end -->


 <script language="javascript" type="text/javascript" src="<?php echo Yii::app()->baseUrl;?>/js/My97DatePicker/WdatePicker.js"></script>
 <script type="text/javascript">
 //选择类型
$('.zcr_type_radio').bind('click',function(){
	var type = $(this).val();
	$('.type_content_1').hide();
	$('.type_content_2').hide();
	$('.type_content_'+type).show();
	$('.del_item').parent().remove();
})
//添加层
function addItem(type){
	var str,acr_value,acr_value_title;
	var condition = $('#zcr_contion').val();
	if(condition.length==0){
		alert("优惠条件不能为空");
		return;	
	}
	
	if(type==1){
		acr_value = $('#tricket_content1').val();
		if(acr_value.length==0){
			alert("优惠内容不能为空");
			return;	
		}
		$('#tricket_content1').val('');
	 	str = '<p><a class="c_06c del_item f_r" href="javascript:" title="">删除</a><span class="fs_14">买家消费满 <input type="hidden" name="CmpPromotion[zcr_condition][condition][]" value="'+condition+'" > <input type="hidden" name="CmpPromotion[zcr_condition][value][]" value="'+acr_value+'" ><strong class="c_f60">'+condition+'</strong> 元 减 <strong class="c_f60">'+acr_value+'</strong> 元 。</span></p>'
	}else if(type==2){
		acr_value = $('#tricket_content2').val();
		if(acr_value.length==0){
			alert("优惠内容不能为空");
			return;	
		}
		acr_value_title =  $('#tricket_content2 option:selected').text();
		str = '<p><a class="c_06c del_item f_r" href="javascript:" title="">删除</a><span class="fs_14">买家消费满<input type="hidden" name="CmpPromotion[zcr_condition][condition][]" value="'+condition+'" > <input type="hidden" name="CmpPromotion[zcr_condition][value][]" value="'+acr_value+'" > <strong class="c_f60">'+condition+'</strong> 元 减 <strong class="c_f60">'+acr_value_title+'</strong> 元 。</span></p>'
	}
	$('#zcr_contion').val('');
	$('#zcr_condition').append(str);
	$('#zcr_contion').removeClass('error');
	$('#tricket_content1').removeClass('error');
}
//删除层
$('.del_item').live('click',function(){
	$(this).parent().remove();
	//alert('d');
});
//提交前的验证，判断有没有填写生效条件
$('#proPromotion-form').bind("submit", function() {
	for(var i=0;i<$('.value_input').length;i++){
		if(!$('.value_input').eq(i).val()>0){
			alert("你输入的价格不正确！");
			return false;
		}
    }
    return true;
});
</script>
 
<!-- 表单验证 -->
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl;?>/js/formvalidata/formValidator-4.1.1.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl;?>/js/formvalidata/formValidatorRegex.js"></script>
 <script>
	jQuery(document).ready(function($) {
		$.formValidator.initConfig({theme:"Default",formID:"proPromotion-form",
			onError:function(msg, obj){
						alert(msg);
						return false;
					},
			onsuccess:function(){ return true; }
		});
	})
</script>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl;?>/js/formvalidata/cmpValidate.js"></script> 